package com.ldl.exam.service.impl;

import com.ldl.exam.dao.ExamPaperDetailMapper;
import com.ldl.exam.dao.QuestionMapper;
import com.ldl.exam.entity.ExamPaperDetail;
import com.ldl.exam.entity.ExamPaperDetailExample;
import com.ldl.exam.entity.QuestionWithBLOBs;
import com.ldl.exam.exception.ExamException;
import com.ldl.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liudaolin
 * @version 1.0
 * @date 2022/10/09 15:54:35
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ExamPaperDetailMapper examPaperDetailMapper;

    @Override
    public void add(QuestionWithBLOBs qt) throws ExamException {
        questionMapper.insertSelective(qt);
    }

    @Override
    public void removeById(List<Integer> questionIdList) {
        ExamPaperDetailExample example = new ExamPaperDetailExample();
        example.or().andQuestionIdIn(questionIdList);
        List<ExamPaperDetail> list = examPaperDetailMapper.selectByExample(example);
        if(!list.isEmpty()) {
            throw new ExamException("题目正在被使用, 不能被删除");
        }
        for (Integer qtId : questionIdList) {
            questionMapper.deleteByPrimaryKey(qtId);
        }
    }

    @Override
    public void modify(QuestionWithBLOBs qt) throws ExamException {
        questionMapper.updateByPrimaryKeySelective(qt);
    }
}
